package com.halobits.thenumbergames;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.halobits.thenumbergames.games.ClassicNumberGame;


public class ClassicActivity extends ActionBarActivity {

  protected ClassicNumberGame cg;
  protected GameBoardHelper gbh;
  protected int gridSize;
  protected int[] board;
  protected Button[] buttons;
  protected LinearLayout linearLayout;
  protected TextView timerTextView;
  protected Handler timerHandler;
  protected Runnable timerRunnable;
  private long startTime;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_classic);
    Intent i = getIntent();
    gridSize = i.getIntExtra(SetupActivity.GRID_SIZE, 2) + 2;

    // game class setup
    cg = new ClassicNumberGame(gridSize);
    cg.setupGame();
    board = cg.getBoard();

    // create a gameboardhelper class and generate an
    // activity with the gameboard
    gbh = new GameBoardHelper(this, gridSize, board);
    gbh.generateLayout(this);

    // this section gets the buttons from the layout and
    // sets their onClick activity
    buttons = gbh.getButtonCollection();

    final Intent congratulateIntent = new Intent(this, CongratulateActivity.class);

    for(int j = 0; j < (gridSize*gridSize); j++) {
      buttons[j].setOnClickListener(
         new View.OnClickListener() {
           @Override
           public void onClick(View v) {
             GameButton b = (GameButton) v;
             int vx = b.getLocation();
             if(cg.doClick(vx)) {
               b.setEnabled(false);
               if(cg.getCurrent() == ((gridSize*gridSize) + 1)) {
                 timerHandler.removeCallbacks(timerRunnable);
                 startActivity(congratulateIntent);
               }
             }
           }
         }
      );
    }


    linearLayout = gbh.getLinearLayout();

    // this section adds a timer text at the bottom of the screen
    timerTextView = new TextView(this);
    timerTextView.setLayoutParams(gbh.getLayoutParams());
    timerTextView.setText("Timer");
    linearLayout.addView(timerTextView);

    // section adds ads to the bottom of the screen
    AdView mAdView = new AdView(this);
    mAdView.setLayoutParams(gbh.getLayoutParams());
    mAdView.setAdUnitId(getString(R.string.admob_id));
    mAdView.setAdSize(AdSize.BANNER);

    linearLayout.addView(mAdView);

    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    setContentView(linearLayout, gbh.getLayoutParams());

    // this section initializes a timer and runs it.
    timerHandler = new Handler();
    timerRunnable = new Runnable() {
      @Override
      public void run() {
        long millis = System.currentTimeMillis() - startTime;
        int seconds = (int) (millis / 1000);
        int minutes = seconds / 60;
        seconds = seconds % 60;

        timerTextView.setText(String.format("%02d:%02d", seconds, millis));

        timerHandler.postDelayed(this, 500);
      }
    };

    startTime = System.currentTimeMillis();
    timerHandler.postDelayed(timerRunnable, 0);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_classic, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
      return true;
    }

    return super.onOptionsItemSelected(item);
  }
}
